package entity

import "time"

var epoch, _ = time.Parse(time.DateTime, "2034-01-01 00:00:00")

type Player struct {
	PID        string `json:"pid"`        // 玩家唯一标识
	Level      uint8  `json:"level"`      // 等级
	Value      uint16 `json:"value"`      // 战力值
	Ranking    int64  `json:"ranking"`    // 名次
	UpdateTime int64  `json:"updateTime"` // 更新时间
}

// getTimeMask 越早的时间戳，通过这个函数得到的数值越大。32bit 足够容纳未来十年信息
func (p *Player) getTimeMask() int64 {
	return epoch.Unix() - time.Now().Unix()
}

func (p *Player) MarshalScore() int64 {
	score := int64(0)
	score = score | p.getTimeMask()
	score = score | (int64(p.Value) << 32)
	score = score | (int64(p.Level) << 48)
	return score
}

func (p *Player) UnmarshlScore(score int64) {
	p.UpdateTime = epoch.Unix() - int64(score&0xFFFFFFFF)
	p.Value = uint16((score >> 32) & 0xFFFF)
	p.Level = uint8((score >> 48) & 0xFF)
}
